﻿using System;
using System.Collections.Generic;
using System.Linq;
using CML.Intercamber.Business.Model;

namespace CML.Intercamber.Business.Dao
{
    public class NewsDao
    {
        public static List<NewsDetail> ListNews(Users connectedUser)
        {
            List<NewsDetail> res;
            using (var context = new IntercamberEntities())
            {
                res = (from x in context.News
                       orderby x.DateCreation descending
                       select new NewsDetail
                       {
                           DateCreation = x.DateCreation,
                           Enabled = x.Enabled,
                           IdEstablishment = x.IdEstablishment,
                           IdLanguage = x.IdLanguage,
                           IdNews = x.IdNews,
                           LblUserCreation = x.Users.FirstName + " " + x.Users.LastName,
                           Message = x.Message,
                           Title = x.Title
                       }).ToList();
                res.ForEach(x =>
                {
                    x.Editable = x.CanChange(connectedUser);
                });
            }
            return res;
        }

        public static News FindNews(long id)
        {
            News res;
            using (var context = new IntercamberEntities())
            {
                res = (from x in context.News
                       where x.IdNews == id
                       select x).FirstOrDefault();
            }
            return res;
        }

        public static List<NewsDetail> ListNewsForUser(Users connectedUser)
        {
            if (connectedUser == null)
                return new List<NewsDetail>();

            List<NewsDetail> res;
            using (var context = new IntercamberEntities())
            {
                res = context.News.Where(x =>
                            x.Enabled &&
                            (x.IdEstablishment == null || x.IdEstablishment == connectedUser.IdEstablishment) &&
                            (x.IdLanguage == null || context.Countries.Any(y => y.IdCountry == connectedUser.IdCountry && y.IdLanguage == x.IdLanguage))
                            ).Select(x => new NewsDetail
                            {
                                DateCreation = x.DateCreation,
                                IdEstablishment = x.IdEstablishment,
                                IdNews = x.IdNews,
                                LblUserCreation = x.Users != null ? x.Users.FirstName + " " + x.Users.LastName : "",
                                Message = x.Message,
                                Title = x.Title
                            }).ToList();
            }
            return res;
        }

        public static void AddUpdateNews(News item, Users connectedUser)
        {
            using (var context = new IntercamberEntities())
            {
                var n = context.News.FirstOrDefault(x => x.IdNews == item.IdNews);

                // verif security
                if (n != null && (connectedUser == null || !n.CanChange(connectedUser)))
                    return;

                if (n == null)
                {
                    item.DateCreation = DateTime.Now;
                    item.IdUserCreation = connectedUser.IdUser;
                    if (item.CanChange(connectedUser))
                        context.News.Add(item);
                }
                else
                {
                    n.IdEstablishment = item.IdEstablishment;
                    n.IdUserCreation = connectedUser.IdUser;
                    n.Message = item.Message;
                    n.Title = item.Title;
                    n.Enabled = item.Enabled;
                    n.IdLanguage = item.IdLanguage;
                }
                context.SaveChanges();
            }
        }

        public static void DeleteNews(long id, Users connectedUser)
        {
            using (var context = new IntercamberEntities())
            {
                var n = context.News.FirstOrDefault(x => x.IdNews == id);
                if (n != null && n.CanChange(connectedUser))
                    context.News.Remove(n);
                context.SaveChanges();
            }
        }

    }
}
